package cn.xinzhi.dao.Impl;

import cn.xinzhi.Util.KeyDaoUtil;
import cn.xinzhi.dao.BaseDao;
import cn.xinzhi.dao.Iyh_loginDao;
import cn.xinzhi.entity.Yh_login;
import com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;


public class Yh_loginDaoImpl implements Iyh_loginDao {

    @Override
    /**
     * 注册
     */
    public int  newLogin(String yh_name, String yh_password, String yh_numder) {
        Connection conn = null;
        PreparedStatement stat = null;
        ResultSet rs = null;
        Yh_login login=null;
        int count =-1;
        if (yh_name==null||yh_password==null){
            return count;
        }
        try {
            conn = BaseDao.getConn();
            String sql = "insert into yh_login values ( ? , ? , ? )";
            stat = conn.prepareStatement(sql);
            //Yh_login yu=new Yh_login();
            stat.setString(1,yh_name);
            stat.setString(2,yh_password);
            stat.setString(3,yh_numder);
            count = stat.executeUpdate();
        } catch (MySQLIntegrityConstraintViolationException s) {
            System.out.println("用户名已存在,请重新注册或者前去登录");
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
//        }finally {
//            BaseDao.closeAll(rs,stat,conn);
//        }
        return count;
    }

    @Override
    /**
     * 登录
     */
    public Yh_login login(String yh_name, String yh_password) {
        PreparedStatement pstat=null;
        Connection conn = null;
        ResultSet rs = null;
        Yh_login login=null;
        try {
            conn =BaseDao.getConn();
            String sql="select yh_name,yh_password,yh_numder from yh_login  where yh_name= ?  and yh_password = ?";
            pstat=conn.prepareStatement(sql);
            pstat.setString(1,yh_name);
            pstat.setString(2,yh_password);
//            String pwd = KeyDaoUtil.RsaPr(yh_password);
            rs=pstat.executeQuery();
            if (rs.next()){
                login=new Yh_login();
                login.setYh_name(rs.getString("yh_name"));
                login.setYh_password(rs.getString("yh_password"));
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return login;

    }

    @Override
    public int alterpassword(String xpassword,String jpassword) {
        PreparedStatement pstat=null;
        Connection conn = null;
        int count=-1;
        try {
            conn=BaseDao.getConn();
            String sql ="update yh_login set yh_password = ? where yh_password= ? ";
            pstat=conn.prepareStatement(sql);
            pstat.setString(1,xpassword);
            pstat.setString(2,jpassword);
            count= pstat.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return count;
    }


}
